import axios from "axios";
// import { useMsgbox, Message } from "element3";
import store from "/@/store/index";
import config from "/@/config";
import { getToken } from "/@/utils/store";

const service = axios.create({
  baseURL: config.baseURL, // url = base url + request url
  timeout: 5000, // request timeout
});

service.interceptors.request.use(
  (config) => {
    if (store.getters.token) {
      config!.headers!["X-Token"] = getToken();
    }

    console.log("config", config);
    return config;
  },
  (error) => {
    console.log(error); // for debug
    return Promise.reject(error);
  },
);

service.interceptors.response.use(
  (response) => {
    const res = response.data;
    // if (res.code !== 20000) {
    //   console.log("接口信息报错", res.message);
    //   return Promise.reject(new Error(res.message || "Error"));
    // }
    return res;
  },
  (error) => {
    console.log(`接口信息报错${error}`);
    return Promise.reject(error);
  },
);

export default service;
